﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Pages
{
    public partial class Default : PageMasterBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.PageBase.IsVerified)
                {
                    divShowNeed.Visible = true;
                }
                bindRequest();
                bindCategory();
                DataBindMyRequest();
            }
        }



        private void bindCategory()
        {
            IList<Model.ServiceCategory> list = BLL.ServiceCategoryBLL.Instance.GetActiveList();
            repCategory.DataSource = list;
            repCategory.DataBind();
            ddlCategory.DataSource = list;
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryName";
            ddlCategory.DataBind();
        }

        //绑定所有需求
        private void bindRequest()
        {
            repRequest.DataSource = BLL.RequestInfoBLL.Instance.GetListByStatusID(6, Convert.ToInt32(Common.Enum.RequestState.Enable).ToString());
            repRequest.DataBind();
        }
        //绑定我的需求
        private void DataBindMyRequest()
        {
            if (base.PageBase.IsVerified)
            {
                IList<Model.RequestInfo> list = BLL.RequestInfoBLL.Instance.GetListByStatusIDOrder(8, base.PageBase.VerifiedInfo.User.UserID);
                rptMyRequest.DataSource = list;
                rptMyRequest.DataBind();
                divShowNeed.Visible = list.Count > 0;
            }
        }
        //删除操作
        protected void imgBtnDelete_Command(object sender, CommandEventArgs e)
        {
            string rId = e.CommandArgument.ToString();
            if (!string.IsNullOrWhiteSpace(rId))
            {
                BLL.RequestInfoBLL.Instance.Delete(rId);
                DataBindMyRequest();
            }
        }
        //添加需求
        protected void ibtnAddRequest_Click(object sender, ImageClickEventArgs e)
        {
            if (base.PageBase.IsVerified)
            {
                if (txtTitle.Text.Trim() == "")
                {
                    System.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "ibtnAddRequest ", "alert('需求信息不能为空！');", true);
                }
                else if (BadWordsFilter.Instance.HasBadWord(txtTitle.Text.Trim()))
                {
                    System.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "ibtnAddRequest ", "alert('信息中含有非法字符！');", true);
                }
                else
                {
                    Model.RequestInfo request = new Model.RequestInfo();
                    request.RequestID = Guid.NewGuid().ToString();
                    request.UserID = base.PageBase.VerifiedUserID;
                    request.CategoryName = ddlCategory.SelectedItem.Value;
                    request.Title = txtTitle.Text.Trim();
                    request.Description = "";
                    request.Price = decimal.Parse(ddlPrice.SelectedItem.Value);
                    request.StatusID = (int)Common.Enum.RequestState.UnTreated;

                    request.RequestFrom = Request.UserHostAddress;


                    BLL.RequestInfoBLL.Instance.Add(request);

                    bindRequest();
                    DataBindMyRequest();
                }
            }
            else
            {
                Response.Redirect(Common.PageBase.URL_LOGIN);
            }
        }


    }
}